from __future__ import print_function, division
import os
import sys
import subprocess
import shutil
import cv2

split_dir = '../data/ucfTrainTestlist/'
data_dir = '../data/UCF-101/'
dst_dir = '../data/UCF101/'
train_txts = ['trainlist0{}.txt'.format(i) for i in range(1,4)]
test_txts = ['testlist0{}.txt'.format(i) for i in range(1,4)]

if not os.path.exists(dst_dir):
    os.mkdir(dst_dir)
    
for i in range(1):
    # dst_dir1 = dst_dir+'split{}/'.format(i+1)
    # if not os.path.exists(dst_dir1):
    #     os.mkdir(dst_dir1)
    # dst_dir1 = dst_dir1 + 'train/'
    # if not os.path.exists(dst_dir1):
    #     os.mkdir(dst_dir1)
    # with open(split_dir+train_txts[i], 'r') as f:
    #     lines = f.readlines()
    #     for line in lines:
    #         filename = line.split(' ')[0]
    #         dirname = filename.split('/')[0]
    #         if not os.path.exists(dst_dir1 + dirname):
    #             os.mkdir(dst_dir1 + dirname)
    #         src = data_dir + filename
    #         dst = dst_dir1 + filename
    #         shutil.copyfile(src, dst)
            
    dst_dir1 = dst_dir+'split{}/'.format(i+1)
    if not os.path.exists(dst_dir1):
        os.mkdir(dst_dir1)
    dst_dir1 = dst_dir1 + 'test/'
    if not os.path.exists(dst_dir1):
        os.mkdir(dst_dir1)
    with open(split_dir+test_txts[i], 'r') as f:
        lines = f.readlines()
        for line in lines:
            filename = line.split(' ')[0].strip()
            dirname = filename.split('/')[0]
            if not os.path.exists(dst_dir1 + dirname):
                os.mkdir(dst_dir1 + dirname)
            src = data_dir + filename
            dst = dst_dir1 + filename
            shutil.copyfile(src, dst)

def class_process(dir_path, dst_dir_path, class_name, maxSize=1024):
    class_path = os.path.join(dir_path, class_name)
    if not os.path.isdir(class_path):
        return

    dst_class_path = os.path.join(dst_dir_path, class_name)
    if not os.path.exists(dst_class_path):
        os.makedirs(dst_class_path)

    for file_name in os.listdir(class_path):
        if '.avi' not in file_name:
            continue
        name, ext = os.path.splitext(file_name)
        dst_directory_path = os.path.join(dst_class_path, name)

        video_file_path = os.path.join(class_path, file_name)

        # skip large files
        # if os.path.getsize(video_file_path) > maxSize * 1000:
        #	continue

        try:
            if os.path.exists(dst_directory_path):
                if not os.path.exists(os.path.join(dst_directory_path, 'image_00001.jpg')):
                    subprocess.call('rm -r \"{}\"'.format(dst_directory_path), shell=True)
                    print('remove {}'.format(dst_directory_path))
                    os.makedirs(dst_directory_path)
                else:
                    continue
            else:
                os.makedirs(dst_directory_path)
                print('making ', dst_directory_path)
        except:
            print(dst_directory_path)
            continue

        # cmd = 'ffmpeg -i \"{}\" -vf scale=-1:240 \"{}/image_%05d.jpg\"'.format(video_file_path, dst_directory_path)
        cmd = 'ffmpeg -i \"{}\" -qscale:v 2 \"{}/image_%05d.jpg\"'.format(video_file_path, dst_directory_path)
        
        subprocess.call(cmd, shell=True)

train_paths = ['../data/UCF101/split{}/train/'.format(i) for i in range(1,4)]
test_paths = ['../data/UCF101/split{}/test/'.format(i) for i in range(1,4)]
dst_train_paths = ['../data/UCF---101/split{}/train/'.format(i) for i in range(1,4)]
dst_test_paths = ['../data/UCF---101/split{}/test/'.format(i) for i in range(1,4)]
for i in range(3):
    # for class_name in os.listdir(train_paths[i]):
    #     class_process(train_paths[i], dst_train_paths[i], class_name)
    for class_name in os.listdir(test_paths[i]):
        class_process(test_paths[i], dst_test_paths[i], class_name)
    break

data_dir = '../data/UCF---101/split1/'

for rootname, dirname, filenames in os.walk(data_dir):
    for filename in filenames:
        print(filename)
        img_file = os.path.join(rootname, filename)
        img = cv2.imread(img_file)
        img = cv2.resize(img, (320, 240))
        cv2.imwrite(img_file, img)